var util = require('../../utils/util.js')
const db = wx.cloud.database()
Page({
  data: {
    order: {},
    orderid: '',
    authority: 0,
    current: 0,
    numList: [{
      name: '申请报修单'
    }, {
      name: '报修单派单'
    }, {
      name: '报修单完成'
    }, {
      name: '用户评价'
    }, ],
  },

  onLoad: function(options) {
    console.log(options)
    const orderid = options.id
    const authority = options.authority
    db.collection('order').doc(orderid).get().then(res => {
      res.data.time = util.formatTime(res.data.submitdate)
      this.setData({
        orderid: orderid,
        authority: authority,
        order: res.data
      })
    })
  },

  showImg: function(event) {
    var that = this;
    console.log(event);
    var imgs = event.currentTarget.dataset.imgs;
    var temp = [];
    for (var index in imgs) {
      temp = temp.concat(imgs[index]);
    }
    wx.previewImage({
      current: event.currentTarget.dataset.currentimg,
      urls: temp,
    })
  },

  onChange(event) {
    var order = this.data.order
    order.score = event.detail
    this.setData({
      order: order
    });
  },

  inputstaff: function(event) {
    var order = this.data.order
    order.staff = event.detail.value
    this.setData({
      order: order
    })
  },

  inputstaffphone: function(event) {
    var order = this.data.order
    order.staffphone = event.detail.value
    this.setData({
      order: order
    })
  },

  inputfeedback: function(event) {
    var order = this.data.order
    order.feedback = event.detail.value
    this.setData({
      order: order
    })
  },

  inputevaluate: function(event) {
    var order = this.data.order
    order.evaluate = event.detail.value
    this.setData({
      order: order
    })
  },

  check() {
    var staff = this.data.order.staff
    var staffphone = this.data.order.staffphone
    if (staff == '' || staffphone == '') {
      return false
    } else {
      return true
    }
  },

  assignform: function(event) {
    if (this.check()) {
      wx.showLoading({
        title: '数据加载中...',
      })
      var phone = wx.getStorageSync('phone');
      wx.cloud.callFunction({
        name: 'orderform',
        data: {
          $url: "update_step2",
          mode: 'updateassign',
          id: this.data.order._id,
          staff: this.data.order.staff,
          staffphone: this.data.order.staffphone,
          feedback: this.data.order.feedback,
          adminphone: phone
        },
        success: res => {
          wx.cloud.callFunction({
            name: 'sendsms',
            data: {
              phone: this.data.order.phone,
              templateId: 386955,
              name: this.data.order.name,
              staff: this.data.order.staff,
              staffphone: this.data.order.staffphone,
            },
            success: res => {
              wx.hideLoading()
              wx.showToast({
                title: '派单成功',
                icon: 'success',
                duration: 2000
              })
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '信息填写不全！',
        icon: 'success',
        duration: 2000
      })
    }
  },

  completeform: function(event) {
    var phone = wx.getStorageSync('phone');
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.cloud.callFunction({
      name: 'orderform',
      data: {
        $url: "update_step3",
        id: this.data.order._id,
        adminphone: phone
      },
      success: res => {
        wx.cloud.callFunction({
          name: 'sendsms',
          data: {
            phone: this.data.order.phone,
            name: this.data.order.name,
            templateId: 386954,
          },
          success: res => {
            wx.hideLoading()
            wx.showToast({
              title: '报修单完成',
              icon: 'success',
              duration: 2000
            })

            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    })
  },

  evaluateform: function(event) {
    wx.cloud.callFunction({
      name: 'orderform',
      data: {
        $url: "update_step4",
        id: this.data.order._id,
        score: this.data.order.score,
        evaluate: this.data.order.evaluate
      },
      success: res => {
        wx.showToast({
          title: '评价完成',
          icon: 'success',
          duration: 2000
        })
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
})